<?php

namespace app\controller\api\energy;

use think\App;
use app\controller\api\Base;
use app\validate\energy\EnergyUsersValidate;
use app\common\repositories\users\UsersRepository;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\energy\EnergyUsersRepository;

class EnergyUsers extends Base
{
    protected $repository;

    public function __construct(App $app, EnergyUsersRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    /**
     * 设备关联用户列表
     * @param int $id
     * @return mixed
     *
     * @author Amos
     * 2024-06-07 17:41
     */
    public function usersList(int $id)
    {
        $param = $this->request->param([
            'property_id' => '',
            'keywords' => '',
        ]);
        $param['energy_id'] = $id;
        [$page, $limit] = $this->getPage();
        $res = $this->repository->geApiUsersList($param, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }


    /**
     * 添加关联用户
     * @param int $id
     * @return mixed
     *
     * @author Amos
     * 2024-06-07 17:41
     */
    public function userAdd(int $id)
    {
        $param = $this->request->param([
            'mobile' => '',
            'remark' => '',
        ]);
        validate(EnergyUsersValidate::class)->scene('add')->check($param);

        $usersRepository = app()->make(UsersRepository::class);
        $userInfo = $usersRepository->getInfoByMobile($param['mobile']);
        if (!$userInfo) {
            return app('api_return')->error($param['mobile'] . ' 用户不存在');
        }

        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $meterInfo = $energyMeterRepository->getSearch([])
            ->where('plan_type', 'in', '2,3')
            ->field('id,property_id,energy_type,plan_type')
            ->find();
        if (!$meterInfo) {
            return app('api_return')->error('设备不存在');
        }

        unset($param['mobile']);
        $param['status'] = 1;
        $param['is_main'] = 1;
        $param['user_id'] = $userInfo['id'];
        $param['energy_id'] = $meterInfo['id'];
        $param['energy_type'] = $meterInfo['energy_type'];
        $param['property_id'] = $meterInfo['property_id'];
        if (isset($meterInfo) && $meterInfo['id'] > 0 && ($meterInfo['plan_type'] == 2 || $meterInfo['status'] == 3)) {
            $this->repository->addInfo($this->request->propertyId, $param, $this->request->userId());
        } else {
            return app('api_return')->error('后付费设备无需绑定');
        }
        return app('api_return')->success('绑定成功');
    }

    /**
     * 删除关联用户
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-19 11:25
     */
    public function delete(int $id)
    {
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return app('api_return')->success('删除成功');
        } else {
            return app('api_return')->error('删除失败');
        }
    }
}